package metrics;

import java.util.List;

import base.Environment;
import base.Environment.Configurations;
import agents.Agent;


/**
 * Proposes a way too see if a unique norm emergence has actually occurred or several quota classes have emerged
 *
 */
public class QuotaDisparity extends AgentMetric {
	Environment env;

	public QuotaDisparity(List<Agent> agents, Environment env){
		super(agents);
		this.env = env;
	}
	
	@Override
	protected double computeScore() {
		double result = 0;
		double stdDev = 0, avg = 0;
		double minQuota, maxQuota;
		double stdDevThreshold = env.getDouble(Configurations.QUOTA_STDDEV_THRESHOLD);

		minQuota = maxQuota	= agents.get(0).getQuota();
		for(Agent x:agents){
			avg += x.getQuota();
			minQuota = (x.getQuota() < minQuota?x.getQuota():minQuota);
			maxQuota = (x.getQuota() > maxQuota?x.getQuota():maxQuota);
		}
		
		avg /= agents.size();
		
		for(Agent x:agents)
			stdDev += Math.pow(x.getQuota() - avg, 2);
		stdDev = Math.sqrt(stdDev / agents.size());
		
		result = Math.ceil((maxQuota - minQuota) / stdDev);
		
		//	Account for perfect norm emergence
		if (result == 0)
			result ++;
		
		//	Account for stdDev too small to build different classes
		if (stdDev < maxQuota*stdDevThreshold)
			result = 1;
			
		return result;
	}
	
	public String toString(){
		String result = "@ Quota Classes: " + computeScore();
		return result;
	}


}
